package com.community.common.pojo.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * http请求返回对象
 *
 * @author Administrator
 */
@Data
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyResponse implements Serializable {

    private static final long serialVersionUID = 6929456187680547881L;
    private String message;
    private Integer code;
    private Object data;


    public MyResponse(CodeEnum codeEnum, String message, Object data) {
        this.code = codeEnum.getCode();
        this.message = message == null ? codeEnum.getMessage() : message;
        this.data = data;
    }

    public MyResponse(CodeEnum codeEnum, Object data) {
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMessage();
        this.data = data;
    }

    public MyResponse(CodeEnum codeEnum, String message) {
        this.code = codeEnum.getCode();
        this.message = message;
    }

    public MyResponse(CodeEnum codeEnum) {
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMessage();
    }

    /**
     * 成功返回
     */
    public static MyResponse success(Object data) {
        return new MyResponse(CodeEnum.SUCCESS, data);
    }

    /**
     * 成功返回
     */
    public static MyResponse success() {
        return new MyResponse(CodeEnum.SUCCESS);
    }

    public static MyResponse success(CodeEnum codeEnum) {
        return new MyResponse(codeEnum);
    }

    /**
     * 成功返回
     */
    public static MyResponse success(String message) {
        return new MyResponse(CodeEnum.SUCCESS, message);
    }

    public static MyResponse success(String message, Object data) {
        return new MyResponse(CodeEnum.SUCCESS, message, data);
    }

    /**
     * 接口处理失败
     */
    public static MyResponse fail(Object data) {
        return new MyResponse(CodeEnum.PROCESSING_FAIL, data);
    }

    /**
     * 接口处理失败
     */
    public static MyResponse fail() {
        return new MyResponse(CodeEnum.PROCESSING_FAIL);
    }

    /**
     * 接口处理失败
     */
    public static MyResponse fail(CodeEnum codeEnum) {
        return new MyResponse(codeEnum);
    }

    /**
     * 接口处理失败
     */
    public static MyResponse fail(String message) {
        return new MyResponse(CodeEnum.PROCESSING_FAIL, message);
    }

    public static MyResponse fail(CodeEnum codeEnum, String message) {
        return new MyResponse(codeEnum, message);
    }


    /**
     * 接口异常
     */
    public static MyResponse exception() {
        return new MyResponse(CodeEnum.SERVICE_EXCEPTION);
    }
}